package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.Element;
import vo.Template;
import vo.User;
import business.ElementService;
import business.SourceService;
import business.TemplateService;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class TemplateAction implements Preparable {
	private TemplateService service;
	private ElementService elementService;
	private UserService userService;
	private SourceService sourceService;
	private List<Template> templates;
	private Template template;

	private User curUser;

	// private long entry;
	// private int state;
	private ArrayList<String> delIds = new ArrayList<String>();

	private String domain;
	private String urlRegex;
	private long sourceId;
	private String contentUrlRegex;
	private int needUpdate;
	// elemnts
	private String author;
	private String ip;
	private String title;
	private String readNum;
	private String commentNum;
	private String pubTime;
	private int fetchInterval;
	private String content;

	private long templateId;

	// 分页的变量
	private int curPage;
	private int totalPage;
	private int pageSize = 5;

	public String listWebs() {
		System.out.println("list web:");
		System.out.println("sourceId:" + sourceId);
		if (curPage == 0)
			curPage = 1;
		System.out.println("curPage:" + curPage);
		int totalCount = (int) service.getTotalCount(sourceId);
		System.out.println("totalCount:" + totalCount);
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);

		this.templates = service.getBySourceId((curPage - 1) * pageSize, pageSize, sourceId);
		System.out.println("template count:" + templates.size());
		return Action.SUCCESS;
	}

	// 显示抽取规则详细页面
	public String listWeb() {
		System.out.println("templateId:" + templateId);
		// System.out.println("sourceId:" + sourceId);
		template = service.getById(templateId);
		// this.templates = service.getAllTemplates();

		// if template exists
		if (template != null) {
			// templateId = template.getId();
			// author
			Element authorEle = elementService.getByTemplateIdName(templateId, "author");
			if (authorEle != null)
				author = authorEle.getDefine();
			// ip
			Element ipEle = elementService.getByTemplateIdName(templateId, "ip");
			if (ipEle != null)
				ip = ipEle.getDefine();

			// title
			Element titleEle = elementService.getByTemplateIdName(templateId, "title");
			if (titleEle != null)
				title = titleEle.getDefine();
			System.out.println("title:" + templateId + "," + title);

			// readnum
			Element readNumEle = elementService.getByTemplateIdName(templateId, "readNum");
			if (readNumEle != null)
				readNum = readNumEle.getDefine();

			// comment
			Element commentEle = elementService.getByTemplateIdName(templateId, "commentNum");
			if (commentEle != null)
				commentNum = commentEle.getDefine();

			// Content
			Element contentEle = elementService.getByTemplateIdName(templateId, "content");
			if (contentEle != null)
				content = contentEle.getDefine();
			System.out.println("content:" + templateId + "," + content);

			// pubTime
			Element pubTimeEle = elementService.getByTemplateIdName(templateId, "pubTime");
			if (pubTimeEle != null)
				pubTime = pubTimeEle.getDefine();

			fetchInterval = template.getFetchInterval();
			this.contentUrlRegex = template.getContentUrlRegex();
		} else {
			// no template id
		}

		System.out.println("display information");
		System.out.println("author:" + author);
		System.out.println("ip:" + ip);
		System.out.println("title:" + title);
		System.out.println("readNum:" + readNum);
		System.out.println("commentNum:" + commentNum);
		System.out.println("pubTime:" + pubTime);
		System.out.println("content:" + content);

		return Action.SUCCESS;
	}

	public String addTemplate() {
		System.out.println("add tp:");
		Template tp = new Template();
		tp.setName("123");
		service.addTemplate(tp);
		return listWeb();
	}

	public String updateTemplate() {
		Template tmp = service.getById(templateId);
		sourceId = tmp.getSource().getId();

		// author
		Element authorObj = elementService.getByTemplateIdName(templateId, "author");
		authorObj.setDefine(author);
		elementService.update(authorObj.getId());

		// ip
		Element ipObj = elementService.getByTemplateIdName(templateId, "ip");
		ipObj.setDefine(ip);
		elementService.update(ipObj.getId());

		// title
		Element titleObj = elementService.getByTemplateIdName(templateId, "title");
		titleObj.setDefine(title);
		elementService.update(titleObj.getId());

		// readNum
		Element readNumObj = elementService.getByTemplateIdName(templateId, "readNum");
		readNumObj.setDefine(readNum);
		elementService.update(readNumObj.getId());

		// commentNum
		Element commentNumObj = elementService.getByTemplateIdName(templateId, "commentNum");
		commentNumObj.setDefine(commentNum);
		elementService.update(commentNumObj.getId());

		// content
		Element contentObj = elementService.getByTemplateIdName(templateId, "content");
		contentObj.setDefine(content);
		elementService.update(contentObj.getId());

		// pubTime
		Element pubTimeObj = elementService.getByTemplateIdName(templateId, "pubTime");
		pubTimeObj.setDefine(pubTime);		
		elementService.update(pubTimeObj.getId());

		return "updateTemplate";
	}

	// 新建抽取规则
	public String saveTemplate() {
		System.out.println("form save template:");

		System.out.println("fetchInterval:" + fetchInterval);
		System.out.println("contentUrlRegex:" + contentUrlRegex);
		System.out.println("sourceId:" + sourceId);

		Template template = new Template();
		template.setDomain("");// no user input
		template.setUrlRegex("");// no user input
		template.setFetchInterval(0);
		template.setContentUrlRegex(contentUrlRegex);
		template.setSource(sourceService.getById(sourceId));
		template.setNeedUpdate(1);// no user input
		service.addTemplate(template);

		// author rule
		Element authorObj = new Element();
		authorObj.setTemplateId(template.getId());
		authorObj.setName("author");
		authorObj.setDefine(author);
		authorObj.setType("XPATH");
		authorObj.setDefine("");
		elementService.addElement(authorObj);

		// ip rule
		Element ipObj = new Element();
		ipObj.setTemplateId(template.getId());
		ipObj.setName("ip");
		ipObj.setDefine(ip);
		ipObj.setType("XPATH");
		ipObj.setDefine("");
		elementService.addElement(ipObj);

		// title rule
		Element titleObj = new Element();
		titleObj.setTemplateId(template.getId());
		titleObj.setName("title");
		titleObj.setDefine(title);
		titleObj.setType("XPATH");
		titleObj.setDefine("");
		elementService.addElement(titleObj);

		// readNum rule
		Element readNumObj = new Element();
		readNumObj.setTemplateId(template.getId());
		readNumObj.setName("readNum");
		readNumObj.setDefine(readNum);
		readNumObj.setType("XPATH");
		readNumObj.setDefine("");
		elementService.addElement(readNumObj);

		// commnentNum rule
		Element commentNumObj = new Element();
		commentNumObj.setTemplateId(template.getId());
		commentNumObj.setName("commentNum");
		commentNumObj.setDefine(commentNum);
		commentNumObj.setType("XPATH");
		commentNumObj.setDefine("");
		elementService.addElement(commentNumObj);
		// content rule
		Element contentObj = new Element();
		contentObj.setTemplateId(template.getId());
		contentObj.setName("content");
		contentObj.setDefine(content);
		contentObj.setType("XPATH");
		contentObj.setDefine("");
		elementService.addElement(contentObj);

		// author rule
		Element pubTimeObj = new Element();
		pubTimeObj.setTemplateId(template.getId());
		pubTimeObj.setName("pubTime");
		pubTimeObj.setDefine(pubTime);
		pubTimeObj.setType("XPATH");
		pubTimeObj.setDefine("");
		elementService.addElement(pubTimeObj);

		return "saveTemplate";
	}

	public String deleteTemplate() {
		System.out.println("delete template:");
		System.out.println("sourceId:" + sourceId);
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted ids：" + delIds.get(i));
			long templateId = Long.parseLong(delIds.get(i));
			service.delete(templateId);

			// 删除级联的elements
			List<Element> elements = elementService.getByTemplateId(templateId);
			for (Element ele : elements) {
				elementService.delete(ele.getId());
			}

		}
		return "templateDeleted";
	}

	public List<Template> getTemplates() {
		return templates;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
		System.out.println("curUser.getLoginName():" + curUser.getLoginName());
	}

	public void setTemplateService(TemplateService service) {
		this.service = service;
	}

	public void setElementService(ElementService elementService) {
		this.elementService = elementService;
	}

	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public String getUrlRegex() {
		return urlRegex;
	}

	public void setUrlRegex(String urlRegex) {
		this.urlRegex = urlRegex;
	}

	public int getFetchInterval() {
		return fetchInterval;
	}

	public void setFetchInterval(int fetchInterval) {
		this.fetchInterval = fetchInterval;
	}

	public long getSourceId() {
		return sourceId;
	}

	public void setSourceId(long sourceId) {
		this.sourceId = sourceId;
	}

	public String getContentUrlRegex() {
		return contentUrlRegex;
	}

	public void setContentUrlRegex(String contentUrlRegex) {
		this.contentUrlRegex = contentUrlRegex;
	}

	public int getNeedUpdate() {
		return needUpdate;
	}

	public void setNeedUpdate(int needUpdate) {
		this.needUpdate = needUpdate;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getReadNum() {
		return readNum;
	}

	public void setReadNum(String readNum) {
		this.readNum = readNum;
	}

	public String getCommentNum() {
		return commentNum;
	}

	public void setCommentNum(String commentNum) {
		this.commentNum = commentNum;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setService(TemplateService service) {
		this.service = service;
	}

	public long getTemplateId() {
		return templateId;
	}

	public void setTemplateId(long templateId) {
		this.templateId = templateId;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public void setSourceService(SourceService sourceService) {
		this.sourceService = sourceService;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getPubTime() {
		return pubTime;
	}

	public void setPubTime(String pubTime) {
		this.pubTime = pubTime;
	}

}